package com.stx.jeepay.payment.channel.plspay.payway;

import com.jeequan.jeepay.exception.JeepayException;
import com.jeequan.jeepay.model.PayOrderCreateReqModel;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import com.stx.jeepay.core.entity.PayOrderEntity;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.model.params.plspay.PlspayConfig;
import com.stx.jeepay.payment.channel.plspay.PlspayKit;
import com.stx.jeepay.payment.channel.plspay.PlspayPaymentService;
import com.stx.jeepay.payment.model.MchAppConfigContext;
import com.stx.jeepay.payment.rqrs.AbstractRS;
import com.stx.jeepay.payment.rqrs.msg.ChannelRetMsg;
import com.stx.jeepay.payment.rqrs.payorder.UnifiedOrderRQ;
import com.stx.jeepay.payment.rqrs.payorder.payway.AliPcOrderRS;
import com.stx.jeepay.payment.utils.ApiResBuilder;
import org.springframework.stereotype.Service;

/**
 * 计全付 支付宝 PC支付
 */
@Service("plspayPaymentByAliPcService")
public class AliPc extends PlspayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrderEntity payOrder) {
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrderEntity payOrder, MchAppConfigContext mchAppConfigContext) {
        // 构造函数响应数据
        AliPcOrderRS res = ApiResBuilder.buildSuccess(AliPcOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        try {
            // 构建请求数据
            PayOrderCreateReqModel model = new PayOrderCreateReqModel();
            // 支付方式
            model.setWayCode(PlspayConfig.ALI_PC);
            // 异步通知
            model.setNotifyUrl(getNotifyUrl());

            // 统一下单
            PayOrderCreateResponse response = PlspayKit.payRequest(payOrder, mchAppConfigContext, model);
            // 下单状态
            Boolean flag = PlspayKit.checkPayResp(response, mchAppConfigContext);
            if (flag) {
                // 下单成功
                String payUrl = response.getData().getString("payData");
                String payDataType = response.getData().getString("payDataType");
                if (CS.PAY_DATA_TYPE.FORM.equals(payDataType)) {
                    // 表单支付
                    res.setFormContent(payUrl);
                } else if (CS.PAY_DATA_TYPE.CODE_IMG_URL.equals(payDataType)) {
                    // 二维码图片
                    res.setCodeImgUrl(payUrl);
                } else {
                    // 默认是url支付
                    res.setPayUrl(payUrl);
                }
                channelRetMsg.setChannelOrderId(response.get().getPayOrderId());
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
            } else {
                // 失败
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                channelRetMsg.setChannelErrCode(response.getCode() + "");
                channelRetMsg.setChannelErrMsg(response.getMsg());
            }
        } catch (JeepayException e) {
            channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
        }
        return res;
    }
}
